#include <iostream>
#include <stdio.h>
#include <cstdlib>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdbool.h>
#include <cstring>

#define SIZE 1024
#define MAX_ARGV 64
#define SEP " "

char *argv[MAX_ARGV];
char pwd[SIZE];
char env[SIZE];
int lastcode = 0;

const char *HostName()
{
    char *hostname = getenv("HOSTNAME");
    if (hostname)
        return hostname;
    else
        return "None";
}

const char *UserName()
{
    char *username = getenv("USER");
    if (username)
        return username;
    else
        return "None";
}

const char *WorkDir()
{
    char *workdir = getenv("PWD");
    if (workdir)
        return workdir;
    else
        return "None";
}

char *Home()
{
    char *home = getenv("HOME");
    return home;
}

int interact(char *out, int size)
{
    std::cout << "[" << UserName() << "@" << HostName()
              << " " << WorkDir() << "]$ ";

    fgets(out, size, stdin);
    out[strlen(out) - 1] = '\0';

    return strlen(out);
}

void execute()
{
    pid_t id = fork();
    if (id == 0)
    {
        execvp(argv[0], argv);
        exit(1);
    }

    int status = 0;
    pid_t rid = waitpid(id, &status, 0);

    if (rid == id)
        lastcode = WEXITSTATUS(status);
}

int main()
{

    return 0;
}

/*
#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

int main()
{
    char *const env[] = {
        (char*)"haha=hehe",
        (char*)"PATH=/",
        nullptr
    };

    std::cout << "I am a process, pid: " << getpid() << std::endl;
    pid_t id = fork();
    if (id == 0)
    {
        extern char **environ;
        sleep(1);
        execle("./mytest", "mytest", nullptr, environ);
        std::cout << "exec end...\n";
        exit(1);
    }

    pid_t rid = waitpid(id, nullptr, 0);
    if (rid > 0)
        std::cout << "wait success\n";
    return 0;
}



#include <iostream>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>

#define NUM 5

typedef void (*fun_t)();
fun_t tasks[NUM];

void printLog()
{
    std::cout << "this is a log print task\n";
}

void printNet()
{
    std::cout << "this is a net task\n";
}

void printNPC()
{
    std::cout << "this is a flush NPC\n";
}

void initTask()
{
    tasks[0] = printLog;
    tasks[1] = printNet;
    tasks[2] = printNPC;
    tasks[3] = nullptr;
}

void excuteTask()
{
    for (int i = 0; tasks[i]; i++)
        tasks[i]();
}

int main()
{
    initTask();
    pid_t id = fork();
    if (id == 0)
    {
        // int a = 10;
        int cnt = 5;
        while (cnt--)
        {
            std::cout << "I am a child process, pid: " << getpid() << ", ppid: " << getppid()
                      << ", cnt: " << cnt << std::endl;
            sleep(1);
        }
        sleep(1);
        int *p = nullptr;
        *p = 100;
        // a /= 0;
        // exit(111);
    }

    int status = 0;
    while (1)
    {
        pid_t rid = waitpid(id, &status, WNOHANG);
        if (rid > 0)
        {
            std::cout << "wait success, rid: " << rid << ", status: " << status
                << ", exit code: " << WEXITSTATUS(status) << std::endl;
            break;
        }
        else if (rid == 0)
        {
            std::cout << "father say: child is running, do other thing\n";
            std::cout << "#############  task begin  #################\n";
            excuteTask();
            std::cout << "#############   task end   #################\n";
        }
        else
        {
            perror("waitpid");
            break;
        }
        sleep(1);
    }
    return 0;
}
*/